package com.yunfei.common.auto.cratePage;


import com.yunfei.common.auto.GeneratorBase;
import com.yunfei.common.auto.util.CapitalizeUtil;
import com.yunfei.common.auto.util.DbInfoUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.List;
import java.util.Map;

import static com.yunfei.common.auto.createClass.GenInterfaceImpl.replaceUnderlineAndfirstToUpper;


/**
 * 自动初始化页面
 * @author  贤云
 *
 */
public class GeneratorEditePage extends GeneratorBase {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(GeneratorBase.DRIVER);
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pass = GeneratorBase.PASS;
            conn = DriverManager.getConnection(url,user,pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args){
        GeneratorEditePage generatorInitPageDB=new GeneratorEditePage();
        generatorInitPageDB.generatorInitPage();
    }
    public  void generatorInitPage(){
        Connection conn=getConnection();
        String sql="select * from "+tablename;
        PreparedStatement stmt;
        String content="";
        String queryParam="";//查询参数
        String datagrid="";
        String  pk="";
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery(sql);
            //ResultSetMetaData data=rs.getMetaData();
            String driver = GeneratorBase.DRIVER;
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pwd =GeneratorBase.PASS;
            //String table = "FZ_USER_T";
            GeneratorBase generatorBase=new GeneratorBase();
            String table = generatorBase.tablename;
            List<Map<String,Object>> data= DbInfoUtil.getTableInfo(driver,url,user,pwd,table);
            DatabaseMetaData dbmd=conn.getMetaData();
            ResultSet  resultSet  = dbmd.getTables(null, "%", tablename, new String[] { "TABLE" });

            while(rs.next()){
                int columnCount=data.size();
                for(int i = 1 ; i<columnCount ; i++){
                    //获得所有列的数目及实际列数
                    //获得指定列的列名
                    String columnName = data.get(i).get("COLUMN_NAME").toString();
                    String columnRemark=data.get(i).get("columnRemark").toString();
                    String dbType=data.get(i).get("dbType").toString();
                    pk=data.get(i).get("pk").toString();
                    String columnRemarkAll=columnRemark;
                    //获得指定列的列值
                    String columnValue = rs.getString(i);

                    String columnLabel= CapitalizeUtil.initcap(columnName.toLowerCase());

                    //是否为空
                    int isNullable=Integer.parseInt(data.get(i).get("isNullable").toString());
                    //判断是否需要添加查询语句
                    boolean isFormElement = !columnRemarkAll.contains("noFormElement");
                    if(isFormElement){
                        boolean isNullableBoolean=false;
                        if(isNullable==1){
                            isNullableBoolean=true;
                        }
                        if(dbType.contains("DATE")) {//处理时间框
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t\t\t\t<input type=\"text\" name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请选择" + columnRemark + "\"  class=\"layui-input fsDate\" dateType=\"datetime\" />\n";
                            } else {
                                datagrid += "\t\t\t\t<input type=\"text\" name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\" placeholder=\"请选择" + columnRemark + "\"  class=\"layui-input fsDate\" dateType=\"datetime\" />\n";

                            }
                            datagrid += "\t\t\t</div>\n" +
                                    "\t\t</div>\n";

                        }
                        else if(dbType.contains("DATETIME")) {//处理选择框
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t\t\t\t<select name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请选择" + columnRemark + "\"  class=\"layui-input fsDate\" dateType=\"datetime\" ><option value=\"\">请选择</option></select>\n";
                            } else {
                                datagrid += "\t\t\t\t<select name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\"  placeholder=\"请选择" + columnRemark + "\"  class=\"layui-input fsDate\" dateType=\"datetime\" ><option value=\"\">请选择</option></select>\n";

                            }
                            datagrid += "\t\t\t</div>\n" +
                                    "\t\t</div>\n";

                        }else if(columnRemarkAll.contains("isTextArea")){
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t\t\t\t<textarea name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请选择" + columnRemark + "\"  class=\"layui-textarea\" dateType=\"datetime\"  height=\"80\"></textarea>\n";
                            } else {
                                datagrid += "\t\t\t\t<textarea name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\"  placeholder=\"请选择" + columnRemark + "\"  class=\"layui-textarea\"  height=\"80\"></textarea>\n";

                            }
                            datagrid += "\t\t\t</div>\n" +
                                    "\t\t</div>\n";
                        }
                        else if(columnRemarkAll.contains("isTextContent")){
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t\t\t\t<textarea name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请选择" + columnRemark + "\"  class=\"fsLayedit\" dateType=\"datetime\"  height=\"80\"></textarea>\n";
                            } else {
                                datagrid += "\t\t\t\t<textarea name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\"  placeholder=\"请选择" + columnRemark + "\"  class=\"fsLayedit\"  height=\"80\"></textarea>\n";

                            }
                            datagrid += "\t\t\t</div>\n" +
                                    "\t\t</div>\n";
                        }
                        else if(columnRemarkAll.contains("isSelect")){//select框处理
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t\t\t<select name=\"userType\" name=\""+columnLabel+"\" id=\"" + columnLabel + "\"  lay-verify=\"required\">\n" +
                                        "\t\t\t\t\t<option value=\"\">--请选择--</option>\n" +
                                        "\t\t\t\t</select>";
                            } else {
                                datagrid += "\t\t\t<select name=\"userType\" name=\""+columnLabel+"\" id=\"" + columnLabel + "\"  >\n" +
                                        "\t\t\t\t\t<option value=\"\">--请选择--</option>\n" +
                                        "\t\t\t\t</select>";
                            }
                            datagrid += "\t\t\t</div>\r\n" +
                                    "\t\t</div>\n";
                        }
                        else if(columnRemarkAll.contains("isRadio")){//radio 选择框
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                datagrid += "\t<input type=\"radio\" name="+columnLabel+" id="+columnLabel+" value=\"1\" title=\"有效\" checked=\"\"><div class=\"layui-unselect layui-form-radio layui-form-radioed\"><i class=\"layui-anim layui-icon\">\uE643</i><div>有效</div></div>\r\n" +
                                        "\t\t\t\t<input type=\"radio\" name="+columnLabel+" id="+columnLabel+" value=\"0\" title=\"无效\"><div class=\"layui-unselect layui-form-radio\"><i class=\"layui-anim layui-icon\">\uE63F</i><div>无效</div></div>\r\n" +
                                        "\t\t\t";
                            } else {
                                datagrid += "\t<input type=\"radio\" name="+columnLabel+" id="+columnLabel+" value=\"1\" title=\"有效\" checked=\"\"><div class=\"layui-unselect layui-form-radio layui-form-radioed\"><i class=\"layui-anim layui-icon\">\uE643</i><div>有效</div></div>\r\n" +
                                        "\t\t\t\t<input type=\"radio\" name="+columnLabel+" id="+columnLabel+" value=\"0\" title=\"无效\"><div class=\"layui-unselect layui-form-radio\"><i class=\"layui-anim layui-icon\">\uE63F</i><div>无效</div></div>\r\n" +
                                        "\t\t\t";
                            }
                            datagrid += "\t\t\t</div>\r\n" +
                                    "\t\t</div>\n";
                        }
                        else{//默认文本框处理
                            datagrid += "\t\t<div class=\"layui-form-item\">\n" +
                                    "\t\t\t<label class=\"layui-form-label\">" + columnRemark + "</label>\n" +
                                    "\t\t\t<div class=\"layui-input-block\">\n";
                            if (!isNullableBoolean) {
                                if(columnRemarkAll.contains("disabled")){
                                    datagrid += "\t\t\t\t<input type=\"text\"  disabled=\"\"  name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请输入" + columnRemark + "\" autocomplete=\"off\" class=\"layui-input\"/>\n";
                                }else{
                                    datagrid += "\t\t\t\t<input type=\"text\" name=\"" + columnLabel + "\" id=\"" + columnLabel + "\" required=\"required\" lay-verType=\"tips\" lay-verify=\"required\" placeholder=\"请输入" + columnRemark + "\" autocomplete=\"off\" class=\"layui-input\"/>\n";
                                }
                            } else {
                                if(columnRemarkAll.contains("disabled")){
                                    datagrid += "\t\t\t\t<input type=\"text\" disabled='disabled' name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\" placeholder=\"请输入" + columnRemark + "\" autocomplete=\"off\" class=\"layui-input\"/>\n";

                                }else{
                                    datagrid += "\t\t\t\t<input type=\"text\" name=\"" + columnLabel + "\" id=\"" + columnLabel + "\"  lay-verType=\"tips\" placeholder=\"请输入" + columnRemark + "\" autocomplete=\"off\" class=\"layui-input\"/>\n";

                                }

                            }
                            datagrid += "\t\t\t</div>\n" +
                                    "\t\t</div>\n";
                        }

                    }
                }
                break;
            }
            /****************************************自动生成init.jsp页面 start******************************************************************************/

            try {
                String controllMapId=CapitalizeUtil.replaceUnderlineAndfirstToUpper(this.tablename.toLowerCase(),"_","");
                content=getInitPageContent(pageTitle, pageOutPath,datagrid,controllMapId, pk);
                File directory = new File("");
                //String outputPath = directory.getAbsolutePath()+ "/web/"+ pageOutPath+"/edit.html";
                String outputPath =/* directory.getAbsolutePath()+ "/web/"+*/ pagePath+"/edit.html";
                File file=new File(outputPath);
                file.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /****************************************自动生成init.jsp页面 end******************************************************************************/
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
    }

    /**
     *生成初始化页面
     * @param title 页面标题
     * @param path s9/v3/pastInvestmentCase 页面所在路径
     * @return
     */
    public static  String getInitPageContent(String title,String path,String datagrid,String controllMapId,String pk){
        String content="";
        String property=replaceUnderlineAndfirstToUpper(pk.toLowerCase(),"_","");//将数据库列转小写，去掉下划线并首字母大写
        content="<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "\t<meta content=\"text/html;charset=UTF-8\"/>\n" +
                "\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"/>\n" +
                "\t<meta http-equiv =\"Pragma\" content = \"no-cache\"/>\n" +
                "\t<meta http-equiv=\"Cache-Control\" content=\"no cache\" />\n" +
                "\t<meta http-equiv=\"Expires\" content=\"0\" />\n" +
                "\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\"/>\n" +
                "\t<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\"/>\n" +
                "\t<meta name=\"apple-mobile-web-app-capable\" content=\"yes\"/>\n" +
                "\t<meta name=\"format-detection\" content=\"telephone=no\"/>\n" +
                "\t<script src=\"https://cdn.bootcss.com/pace/1.0.2/pace.min.js\"></script>\n" +
                "\t<link href=\"https://cdn.bootcss.com/pace/1.0.2/themes/pink/pace-theme-flash.css\" rel=\"stylesheet\">\n" +
                "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../plugins/layui/css/layui.css\" media=\"all\"/>\n" +
                "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../css/fs.css\" media=\"all\"/>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/layui/layui.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/jquery/jquery.min.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/fsDict.js?v=1.5.0\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/fs.js?v=1.5.0\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/frame.js?v=1.5.0\"></script>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div class=\"layui-fluid\">\n" +
                "\t<form class=\"layui-form\" id=\"edit_form\" isLoad=\"1\" loadUrl=\"../../"+controllMapId+"/form.shtml\">\n" +
                "\t\t<input type=\"hidden\" name=\""+property+"\"/>\n"+
                "" +datagrid+"\n"+
                "\t\t<hr/>\n" +
                "\t\t<div class=\"layui-form-item\" style=\"text-align: center;\">\n" +
                "\t\t\t<button class=\"layui-btn\" lay-submit=\"\" lay-filter=\"edit\" url=\"/"+controllMapId+"/save.shtml\">编辑</button>\n" +
                "\t\t\t<button type=\"button\" class=\"layui-btn layui-btn-primary\" function=\"close\">关闭</button>\n" +
                "\t\t</div>\n" +
                "\t</form>\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>\n";
        return content;
    }
}
